package com.lipuwater.agent.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lipuwater.agent.R;
import com.lipuwater.agent.app.base.ui.BaseFragment;
import com.lipuwater.agent.ui.activity.FeedbackActivity;
import com.lipuwater.agent.ui.activity.MainActivity;
import com.lipuwater.agent.ui.activity.MessageListActivity;
import com.lipuwater.agent.ui.activity.UserInfoActivity;
import com.lipuwater.agent.ui.event.KeyDownEvent;
import com.lipuwater.agent.ui.event.UserInfoEvent;
import com.lipuwater.agent.utils.LoaderImage;
import com.lipuwater.agent.utils.onclick.NoDoubleClick;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

import static com.lipuwater.agent.R.id.image_head;

/**
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃永无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * Created by  Administrator  on 2017/2/16.
 * Email:924686754@qq.com
 */
public class FragmentMy extends BaseFragment {

    @Bind(R.id.text_username)
    TextView textUsername;
    @Bind(image_head)
    ImageView imageHead;

    private View view;
    private NoDoubleClick mNoDoubleClick = new NoDoubleClick();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        view = inflater.inflate(R.layout.fragment_my, null);
        ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        mContentView.addView(view);
        init();
    }

    private void init() {
        textUsername.setText(userName);
        LoaderImage.onLoadingImage(thumbPath, imageHead, R.drawable.logo);
    }


    @OnClick({R.id.layout_message, R.id.layout_user_info, R.id.image_head, R.id.layout_help_or_feedback})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_message:
                if (mNoDoubleClick.check()) return;
                startActivity(new Intent(getActivity(), MessageListActivity.class));
                break;
            case R.id.layout_user_info:
                if (mNoDoubleClick.check()) return;
                startActivity(new Intent(getActivity(), UserInfoActivity.class));
                break;
            case R.id.image_head:
                startActivity(new Intent(getActivity(), UserInfoActivity.class));
                break;
            case R.id.layout_help_or_feedback:
                if (mNoDoubleClick.check()) return;
                //startActivity(new Intent(getActivity(), HelpOrFeedbackActivity.class));
                startActivity(new Intent(getActivity(), FeedbackActivity.class));
                break;
        }
    }

    public void onEventMainThread(KeyDownEvent event) {
        if (event.getPosition() == 2) {
            if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)) {
                ((MainActivity) getActivity()).exit();
            }
        }
    }

    /**
     * 用户信息更新时自动调用
     *
     * @param event
     */
    public void onEventMainThread(UserInfoEvent event) {
        textUsername.setText(event.getUserInfoBean().getName());
        LoaderImage.onLoadingImage(event.getUserInfoBean().getThumbPath(), imageHead, R.drawable.logo);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
    }


}
